﻿using AutoMapper;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.Traceability.Domain;
using B.S.Traceability.Infrastructure.Interface;
using B.S.Traceability.Write.API.Applicantion.Command.GoodsCommand;
using MediatR;

namespace B.S.Traceability.Write.API.Applicantion.CommandHandler.GoodsCommandHandler
{
    public class AddGoodsCommandHandler : IRequestHandler<AddGoodsCommand, APIResult<int>>
    {
        private readonly IBaseRepository<SU_GoodsInfo> goodRep;
        private readonly IMapper mapper;
        public AddGoodsCommandHandler(IBaseRepository<SU_GoodsInfo> goodRep, IMapper mapper)
        {
            this.goodRep = goodRep;
            this.mapper = mapper;
        }

        public Task<APIResult<int>> Handle(AddGoodsCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result=new APIResult<int>();
            var entity=mapper.Map<SU_GoodsInfo>(request);
            entity.CreateTime=DateTime.Now;
            entity.ProductionCode = DateTime.Now.ToString("yyyyMMddHHmm");
            entity.ProNumber = "SC" + DateTime.Now.ToString("yyyyMMddHHmm");
            var res=goodRep.Add(entity).Result;
            if (res>0)
            {
                result.Code = APIEnum.添加成功;
                result.Msg = APIEnum.添加成功.ToString();
                result.data = res;
            }
            else
            {
                result.Code = APIEnum.添加失败;
                result.Msg = APIEnum.添加失败.ToString();
                result.data = res;
            }
            return Task.FromResult(result);
        }
    }
}
